<?
$sub_menu = "230560";
include_once('_common.php');

auth_check($auth[$sub_menu], "r");

if (empty($fr_date)) // 시작날짜 초기화
    $fr_date = date("Y-m-01", $g4[server_time]);

if (empty($to_date)) // 종료날짜 초기화
    $to_date = date("Y-m-t", $g4[server_time]);

$qstr = "fr_date=$fr_date&to_date=$to_date";

$g4[title] = '결제횟수 통계';

$colspan = 6;

include_once("$g4[admin_path]/admin.head.php");
?>

<?=subtitle($g4[title])?>

<script type="text/javascript">
function fcount_submit(ymd, sdate, edate) 
{
    var f = document.fcount;
    f.ymd.value = ymd;
    if (sdate) f.fr_date.value = sdate;
    if (edate) f.to_date.value = edate;
    f.action = "<?=$PHP_SELF?>";
    f.submit();
}
</script>

<form name=fcount method=get style="margin:0;">
<input type=hidden name="ymd">
<table width=100% cellpadding=3 cellspacing=1>
<tr>
    <td>
        기간 : 
        <input type='text' name='fr_date' size=11 maxlength=10 value='<?=$fr_date?>' class=ed> -
        <input type='text' name='to_date' size=11 maxlength=10 value='<?=$to_date?>' class=ed>
        &nbsp;
        <input type=button class=btn1 value=' 검색 ' onclick="fcount_submit('d');">
        <input type=button class=btn1 value=' 이번달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", $g4[server_time])?>', '<?=date("Y-m-t", $g4[server_time])?>');">
        <input type=button class=btn1 value=' 지난달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?=date("Y-m-t", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 다음달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?=date("Y-m-t", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 상반기 '
            onclick="fcount_submit('m', '<?=date("Y-01-01")?>', '<?=date("Y-06-30")?>');">
        <input type=button class=btn1 value=' 하반기 '
            onclick="fcount_submit('m', '<?=date("Y-07-01")?>', '<?=date("Y-12-31")?>');">
        <input type=button class=btn1 value=' 올해 '
            onclick="fcount_submit('m', '<?=date("Y-01-01")?>', '<?=date("Y-12-31")?>');">

        <input type=button class=btn1 value=' 전체 ' onclick="fcount_submit('d', '2000-01-01', '<?=date("Y-m-t", $g4[server_time])?>')">
        &nbsp;&nbsp;
    </td>
</tr>
</table>
</form>


<table width=100% cellpadding=0 cellspacing=1 border=0>
<colgroup width=100>
<colgroup width=100>
<colgroup width=80>
<colgroup width=''>
<tr><td colspan='<?=$colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td>결제횟수</td>
    <td>회원수</td>
    <td>비율(%)</td>
    <td>그래프</td>
</tr>
<tr><td colspan='<?=$colspan?>' class='line2'></td></tr>
<?
$list = array();
$max = $sum = $sum_cash = 0;

$sql_select = " mb_id, cl_paytype, sum(cl_cash) as cash, sum(cl_payment) as pay, count(*) as cnt";
$sql_common = " from $mw_cash[cash_list_table] ";
$sql_search = " where cl_datetime between '$fr_date 00:00:00' and '$to_date 23:59:59' ";
$sql_search .= " and cl_cash > 0 and cl_payment > 0 and cl_status = '1' ";
$sql_search .= " and cl_paytype <> '포인트' and cl_paytype <> '서비스' and cl_paytype <> '페이팔'";
$sql_group  = " group by mb_id";
if ($ord == "cnt")
    $sql_order = " order by cnt desc ";
else
    $sql_order = " order by pay desc ";

switch ($ymd) {
    case "y":
        $sql_select .= ", left(cl_datetime, 4) as day ";
        break;
    case "m":
        $sql_select .= ", left(cl_datetime, 7) as day  ";
        break;
    default:
	$ymd = "d";
        $sql_select .= ", left(cl_datetime, 10) as day  ";
        break;
}

$sql = "select
	$sql_select
        $sql_common 
        $sql_search 
        $sql_group 
        $sql_order";
$qry = sql_query($sql);

$arr = array();
for ($i=0; $row=sql_fetch_array($qry); $i++)
{
    $sum++;

    if ($row[cnt] > 0)
        $arr[$row[cnt]]++;

    if ($max < $arr[$row[cnt]])
        $max = $arr[$row[cnt]];
}

$total_count = count($list);

/*
$rows = $config[cf_page_rows];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if (!$page) $page = 1; // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함
*/

//for ($i=$from_record; $i<$from_record+$rows; $i++) { 
$i=0;
if (count($arr)) {
    krsort($arr);
    foreach ($arr as $key => $val) {

        $per1 = @round($val / $sum * 100);
        $per2 = @round($val / $max * 100);

        $l = ($i%2);
        echo "
        <tr class='list$l col1 ht center'>
            <td> $key </td>
            <td> $val </td>
            <td> $per1% </td>
            <td align=left> 
                <img src='$g4[admin_path]/img/graph.gif' width='$per2%' height='18'>
            </td>
        <tr>";
        $i++;
    }
}

if ($i == 0)
    echo "<tr><td colspan='$colspan' height=100 align=center>자료가 없습니다.</td></tr>"; 
else {
    echo "<tr><td colspan='$colspan' class='line2'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td>합계</td>
    <td>".number_format($sum)."</td>
    <td>100%</td>
    <td>&nbsp;</td>
</tr>";
}

echo "<tr><td colspan='$colspan' class='line1'></td></tr>";
echo "</table>";

$page = get_paging($config[cf_write_pages], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&domain=$domain&page=");
if ($page) {
    echo "<table width=100% cellpadding=3 cellspacing=1><tr><td align=right>$page</td></tr></table>";
}

include_once("$g4[admin_path]/admin.tail.php");
?>
